{% extends "base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="fas fa-tools me-2"></i>设备管理</h5>
            <a href="{{ url_for('device.device_add') }}" class="btn btn-primary">
                <i class="fas fa-plus me-1"></i>添加设备
            </a>
        </div>
        <div class="card-body">
            <!-- 搜索表单 -->
            <form method="GET" action="{{ url_for('device.device_list') }}" class="mb-4">
                <div class="row g-3">
                    <div class="col-md-4">
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-search"></i></span>
                            <input type="text" class="form-control" name="search" value="{{ search }}" placeholder="搜索设备ID、名称或位置">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" name="device_type">
                            <option value="">所有类型</option>
                            <option value="sensor" {% if device_type == 'sensor' %}selected{% endif %}>传感器</option>
                            <option value="pump" {% if device_type == 'pump' %}selected{% endif %}>水泵</option>
                            <option value="valve" {% if device_type == 'valve' %}selected{% endif %}>阀门</option>
                            <option value="meter" {% if device_type == 'meter' %}selected{% endif %}>流量计</option>
                            <option value="other" {% if device_type == 'other' %}selected{% endif %}>其他</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" name="status">
                            <option value="">所有状态</option>
                            <option value="online" {% if status == 'online' %}selected{% endif %}>在线</option>
                            <option value="offline" {% if status == 'offline' %}selected{% endif %}>离线</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-1"></i>搜索
                        </button>
                    </div>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>设备ID</th>
                            <th>名称</th>
                            <th>类型</th>
                            <th>位置</th>
                            <th>状态</th>
                            <th>最后在线</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for device in devices %}
                        <tr>
                            <td>{{ device.device_id }}</td>
                            <td>{{ device.name }}</td>
                            <td>
                                {% if device.device_type == 'sensor' %}传感器
                                {% elif device.device_type == 'pump' %}水泵
                                {% elif device.device_type == 'valve' %}阀门
                                {% elif device.device_type == 'meter' %}流量计
                                {% else %}其他{% endif %}
                            </td>
                            <td>{{ device.location }}</td>
                            <td>
                                {% if device.status == 'online' %}
                                <span class="badge bg-success">在线</span>
                                {% else %}
                                <span class="badge bg-danger">离线</span>
                                {% endif %}
                            </td>
                            <td>{{ device.last_online.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>
                                <a href="{{ url_for('device.device_edit', id=device.id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="{{ url_for('device.device_delete', id=device.id) }}" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除这个设备吗？');">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="7" class="text-center">暂无设备数据</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('device.device_list', page=pagination.prev_num, search=search, device_type=device_type, status=status) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('device.device_list', page=page, search=search, device_type=device_type, status=status) }}">{{ page }}</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('device.device_list', page=pagination.next_num, search=search, device_type=device_type, status=status) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 